﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using BlueFinity.mvNET.CoreObjects;
using mvLibrary;
using IBMU2.UODOTNET;

namespace fenk
{
    public partial class EditorProgramas : Form
    {
        public EditorProgramas()
        {
            InitializeComponent();
            this.cboOpenWith.SelectedIndex = 0;
        }

        private const string BUFFER_LOCATION = @"C:\Pick\EditorProgramas\{0}";

        private string GetFilePath(string fileName) { return string.Format(BUFFER_LOCATION, fileName); }

        private string errorMessage { get; set; }
        public UniSession UniAccount { get; set; }

        public string User { get; set; }

        public void ServerConnect(string operationType)
        {
            string itemId = txtItemId.Text;
            string fileName = txtPickFileName.Text;
            string programCode = this.OpenBufferFile(itemId);
            string item = programCode.Replace(DataBASIC.CRLF, DataBASIC.AM);
            try
            {
                UniSubroutine sub = UniAccount.CreateUniSubroutine("EDITOR.NET", 6);
                string itemsList = string.Empty;

                sub.SetArg(0, operationType);
                sub.SetArg(1, fileName);
                sub.SetArg(2, itemId);
                sub.SetArg(3, item);
                sub.SetArg(4, User);

                sub.Call();

                item = sub.GetArgDynArray(3).ToString();
                errorMessage = sub.GetArgDynArray(5).ToString();

            }
            catch (Exception mvEx)
            {
                errorMessage = mvEx.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                if (operationType != "C" & operationType != "B")
                {
                    MessageBox.Show("Error:\nMensaje de servidor: " + errorMessage);
                }
            }
            string text = item.Replace(DataBASIC.AM, DataBASIC.CRLF);

            if (operationType.Equals("R"))
            {
                InitializeTextEditorProgram(text, itemId);
            }
        }

        #region Button Actions


        private void btnSave_Click(object sender, EventArgs e)
        {
            ServerConnect("W");
        }

        private void openButton_Click_1(object sender, EventArgs e)
        {
            if (ValidateRequiredFields())
            {
                ServerConnect("R");
            }
        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            if (ValidateRequiredFields())
            {
                ServerConnect("B");
                ShowCompileMessage();
            }
        }


        private void ShowCompileMessage()
        {
            MessageBox.Show(errorMessage.Replace(DataBASIC.AM, DataBASIC.CRLF), "Resultado compilación", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion

        #region File and NotePad

        private string OpenBufferFile(string fileName)
        {
            try
            {
                return File.ReadAllText(GetFilePath(fileName));
            }
            catch (FileNotFoundException)
            {
                return string.Empty;
            }
        }

        private void InitializeTextEditorProgram(string text, string fileName)
        {
            var filePath = GetFilePath(fileName);
            File.WriteAllText(filePath, text);

            Process.Start(this.cboOpenWith.SelectedItem + ".exe", filePath);

        }

        #endregion

        private bool ValidateRequiredFields()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtPickFileName.Text))
            {
                MessageBox.Show("Ingrese archivo (full path)", "Leer item");
                txtPickFileName.Focus();
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtItemId.Text))
            {
                MessageBox.Show("Ingrese clave del registro(item id)", "Leer item");
                txtPickFileName.Focus();
                isValid = false;
            }

            return isValid;
        }


    }

}
